package com.mingqijia.gassafety.shared.vcc;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.gap.basic.exception.BizException;
import com.mingqijia.gassafety.shared.constant.UrlConst;
import com.mingqijia.gassafety.shared.utils.HttpUtil;
import jdk.nashorn.internal.parser.Token;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Map;

/**
 * VCC授权配置
 * @author Damon S.
 * @version v1.0.1
 * @date 2020年07月12日 15:45
 */
@Data
@Component
@Slf4j
@ConfigurationProperties(prefix = "vcc.open-h5client")
public class VccH5ClientConfig {

    @JsonIgnore
    private String server;
    private String grantType;
    private String openAppId;
    private String openAppSecret;


    private static final String TEMPLATE_CHECK_TOKEN = "%s/vcc-oauth/oauth/authorize2/checkToken?token=%s";
    //private static final String TEMPLATE_BIND_LIST = "%s/nv1/vcc-cbs/usersubs/queryBindList?orgId=%s&token=%s";
    //private static final String TEMPLATE_QUERY_PHONE = "%s/nv1/vcc-cbs/usersubs/getLoginUserInfo?token=%s";

    public String getToken() {
        String token = "";
        Map<String, Object> params = new HashMap<>();
        params.put("openAppId", openAppId);
        params.put("openAppSecret", openAppSecret);
        try {
            String body = com.mingqijia.gassafety.shared.utils.HttpUtil.getBody(com.alibaba.fastjson.JSONObject.toJSONString(params),
                    server + UrlConst.OPEN_TOKEN.getUrl(), "", "");
            if (StringUtils.isEmpty(body)) return null;
            log.info("getToken-response:{}", body);
            com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(body);
            if (Integer.parseInt(jsonObject.getString("code")) == 0) {
                com.alibaba.fastjson.JSONObject data = jsonObject.getJSONObject("data");
                token = data.getString("token");
            }
        } catch (Exception e) {
            log.error("getToken-error:{}", e);
            return null;
        }
        return token;
    }
}


